import { Controller } from 'egg';

class ProjectController extends Controller {
    /**
     * 创建考核项目
     */
    public async createProject() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                admin_id:'string',
                project_name: 'string',
                project_min:'string',
                project_max:'string',
                project_is_self:'string',
                project_start_time:'string',
                project_end_time:'string',
                employee_id:'string',
                target_employee_id:'string',
                position_list:'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const project_min=Number(request.body.project_min);
        const project_max=Number(request.body.project_max);
        if(!project_max||project_max<=0){
            ctx.body = ctx.helper.responseErrorParam('分数格式错误');
            return;
        }
        if(!project_min||project_min<=0){
            ctx.body = ctx.helper.responseErrorParam('分数格式错误');
            return;
        }
        try{
            JSON.parse(request.body.position_list);
        }catch(e){
            ctx.body = ctx.helper.responseErrorParam('职位加分规则格式错误');
            return;
        }
        await ctx.service.projectService.createProject(request.body);
        ctx.body = ctx.helper.responseSuccess('创建成功');
    }

    /**
     * 查找考核项目
     */
    public async searchProject() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                page:'string',
                size:'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const page=Number(request.body.page);
        const size=Number(request.body.size);
        if(!size||size<=0){
            ctx.body = ctx.helper.responseErrorParam('一页显示数格式错误');
            return;
        }
        if(!page||page<=0){
            ctx.body = ctx.helper.responseErrorParam('页码格式错误');
            return;
        }
        const result = await ctx.service.projectService.searchProject(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 根据项目id查找项目信息
     */
    public async searchProjectById() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                project_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.projectService.searchProjectById(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 修改项目
     */
    public async updateProject() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                admin_id:'string',
                project_id:'string',
                project_name: 'string',
                project_min:'string',
                project_max:'string',
                project_is_self:'string',
                project_start_time:'string',
                project_end_time:'string',
                employee_id:'string',
                target_employee_id:'string',
                position_list:'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const project_min=Number(request.body.project_min);
        const project_max=Number(request.body.project_max);
        if(!project_max||project_max<=0){
            ctx.body = ctx.helper.responseErrorParam('分数格式错误');
            return;
        }
        if(!project_min||project_min<=0){
            ctx.body = ctx.helper.responseErrorParam('分数格式错误');
            return;
        }
        try{
            JSON.parse(request.body.position_list);
        }catch(e){
            ctx.body = ctx.helper.responseErrorParam('职位加分规则格式错误');
            return;
        }
        await ctx.service.projectService.updateProject(request.body);
        ctx.body = ctx.helper.responseSuccess('修改成功');
    }

    /**
     * 启动和暂停项目
     * @param param 
     */
    public async startAndStopProject(){
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                project_id: 'string',
                admin_id:'string',
                type:'type'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.projectService.startAndStopProject(request.body);
        ctx.body = ctx.helper.responseSuccess('操作成功');
    }

    /**
     * 查找前十名以及最高分，最低分，平均分
     * @param param 
     */
    public async searchTopTenAndAvg(){
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                project_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result=await ctx.service.projectService.searchTopTenAndAvg(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功',result);
    }

    /**
     * 查找被评分人列表
     */
    public async searchTargetEmployeeList(){
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                page:'string',
                size:'string',
                project_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result=await ctx.service.projectService.searchTargetEmployeeList(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功',result);
    }

    /**
     * 查找谁评的分详情列表
     */
    public async searchDetailListByEmployee(){
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                project_id: 'string',
                employee_id:'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result=await ctx.service.projectService.searchDetailListByEmployee(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功',result);
    }

    /**
     * 查找他的打分详情列表
     */
    public async searchDetailListByTargetEmployee(){
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                project_id: 'string',
                employee_id:'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result=await ctx.service.projectService.searchDetailListByTargetEmployee(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功',result);
    }

    /**
     * 随机查找项目模板
     */
    public async searchProjectTemplet(){
        const { ctx } = this;
        const result=await ctx.service.projectService.searchProjectTemplet();
        ctx.body = ctx.helper.responseSuccess('查询成功',result);
    }

    /**
     * 查找剩余考核人列表
     */
    public async searchSurplus(){
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                project_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result=await ctx.service.projectService.searchSurplus(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功',result);
    }

    /**
     * 查找已经考核人列表
     */
    public async searchHasGradedList(){
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                project_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result=await ctx.service.projectService.searchHasGradedList(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功',result);
    }
}
export default ProjectController;